#ifndef H_A2GRTXT
#define H_A2GRTXT

#ifndef H_A2SYSDEF
# include "a2sysdef.h"
#endif

#ifdef __STRIP_TV__
# /* include "a2tv0win.h" */
# else

#ifndef H_A2SYSTEM
# include "a2system.h"
#endif

/*------ a2grtxt.h - for displays  ----------------------*/

#define A2TV_ON  1
#define A2TV_OFF 0
#define A2TV_GET A2TV_ASK
#define A2TV_ASK (-1717)
#define A2TV_SET 2

#ifndef H_A2SYSDEF
# include "a2sysdef.h"
#endif

#ifndef H_A2GRDEF
# include "a2grdef.h"
#endif

#ifndef H_A2TVDEF
# include "a2tvdef.h"
#endif

#if !defined(__FOR_DOS__) && !defined(__FOR_DPMI16__)
# define NOGR
#endif

typedef struct { TINT p; TINT s; } TV0_WIN;

typedef struct {
   int on;
   TINT pos;
   int size;
   int grwON;
   union {
      TV0_WIN tw;
#ifndef NOGR
      GrW gw;
#endif
   } win;
} A2CINF;

typedef struct {
  BYTE  v;  /* vertical line (left) */
  BYTE  h;  /* horisotal line (up)  */
  BYTE ul;  /* upper left corner    */
  BYTE ur;  /* upper right corner   */
  BYTE ll;  /* lower left corner    */
  BYTE lr;  /* lower right corner   */
  BYTE v1;  /* vertical line (right)*/
  BYTE h1;  /* horisotal line (down)*/
} A2FRAME;

typedef struct {
   BYTE l_brk,r_brk, c_sys,c_min,c_max,c_rst;
   BYTE u_arrow,d_arrow,l_arrow,r_arrow, h_norm,h_tumb, v_norm,v_tumb;
   BYTE u_half,d_half;
} A2BRDEXT;

#ifndef TV000
# define CT_ALT  0
# define CT_MAIN 1
# ifdef A2TV0PC_SOURCE_FILE
    extern int A2CODE_T =  CT_ALT;
# else
    extern int A2CODE_T;
# endif
# define CODE_T A2CODE_T
#endif

/*-------------- graphic window filler --------*/
#define G_COD  0x01               /*   cod     */
#define G_ATR  0x00               /*   atr     */
#define G_WORD (G_COD+(G_ATR<<8)) /* full word */

/*----------------- error codes ---------------------------*/
#define ERR_BADMODE -10              /* a2tv0sm            */
#define ERR_FNTFILE -12              /* a2tv0sm            */
#define ERR_FNTMEM  -13              /* a2tv0sm            */

#define bd_GMODE    -20 /* bad video mode      a2tv0stgw, -*/
#define bd_GMEM     -21 /* not enough memory   a2tv0stg,   */
#define bd_GBUF     -22 /* bad buffer          a2tv0rsgf,  */
#define bd_GFILE    -23 /* errors in file      a2tv0rsg    */
#define bd_GBLK     -24 /* bad block in file   ------------*/

#ifndef DEF_TINT_MACROS
#define DEF_TINT_MACROS
# define GETWIN(x)       MOVETINT(x.p,window.p); MOVETINT(x.s,window.s)
# define MOVETINT(x,y)   x=y
# define SUBTTINT(z,x,y) z.row=(int)(x.row-y.row); z.clm=(int)(x.clm-y.clm)
# define ADDTINT(z,x,y)  z.row=(int)(x.row+y.row); z.clm=(int)(x.clm+y.clm)
# define SETTINT(x,a,b)  x.row=a; x.clm=b
#endif

/*-------------------- Global data ------------------------*/
#ifndef TV000
   extern A2FRAME A2frame[5][2];
#endif

#ifdef __cplusplus
extern "C" {
#endif


/*----- a2tvmvm - move n bytes to/from screen/buffer ------*/
/* mode:                                  =(A2W)= 04/03/91 */
#define MV_LIN 0 /* put line to screen/buffer (default)    */
#define MV_COL 1 /* put column to screen/buffer            */
#define MV_GET 4 /* get line from screen/buffer            */
#define MV_SAV 5 /* save full screen to buffer (sbuf)      */
#define MV_RCL 6 /* reclame full screen from buffer (sbuf) */
void a2tvmvm(int scrpos,void *line,int n,int op); /* */

/*---------------------------------------------------------*/
/*-------------------- cursor control ---------------------*/
int a2tv0cur(TINT cur);            /* set cursor position */
int a2tv0csz(int size);  /*set curs size (! -1,-2,A2TV_ASK)*/
int a2tv0con(int viflg); /*curs on/off/ask(A2TV_ON/OFF/ASK)*/
void a2tv0cinf(int mode, A2CINF *cinf); /* get/set cursor  */
/*----------------------------------- state (A2TV_ASK/SET) */

#ifndef TV000
/*--------------- graphic cursor control ------=(A2W)=-----*/
# ifndef NOGR
 void a2tv0cop(void); /*activate cursor for graphic modes  */
 void a2tv0ccl(void); /*deactivate cursor for graphic modes*/
 void a2tv0cup(void); /*update cursor for graphic modes    */
# else
#  define a2tv0cop()
#  define a2tv0ccl()
#  define a2tv0cup()
# endif
#endif

/*---------------------------------------------------------*/
/*------------------- video mode control ------------------*/
int a2tv0op(void);                    /* screen open     */
void a2tv0cl(void);                     /* screen close    */
int a2tv0sm(int mode, int save);  /* set video mode  */
MDESC *a2tv0tm (int mode);            /* test video mode */

/*---------------------------------------------------------*/
/*-------------------- window control ---------------------*/
void a2tv0wnd(TINT ul,TINT s); /* set current window       */
#ifndef TV000
#if defined(__FOR_DOS__) || defined(__FOR_DPMI16__)
int a2tv0sGrW (GrW *gw);      /* set current gr window    */
#endif
void a2tv0scr(TINT ul,TINT s); /* set logical screen s.row=0 -- phis scr*/
#endif

void *a2tv0strw(int flags);       /* get buf & save window */
int a2tv0str(void *b);            /* refresh window buffer */
int a2tv0rst(void *b);            /* display window buffer */
int a2tv0rstf(void *b);           /* display & free buffer */
int a2tv0atr(int atr);            /* change window atr     */
int a2tv0clw(int atr,int filler); /* clear window          */
int a2tv0deplON (int on_off);     /* enbl/dsbl deploding   */

#define BRD_00     0x0005    /*  type of frame            */
#define BRD_11     0x0001
#define BRD_12     0x0002
#define BRD_21     0x0003
#define BRD_22     0x0004
#define BRD_SHAD   0x0008    /*   shadow                   */
#define BRD_TUL    0x0010    /*   text in left             */
#define BRD_TUC    0x0020    /*   text in center           */
#define BRD_TUR    0x0030    /*   text in right            */
#define BRD_TUP    0x0040    /* text in top/ 0 - in bottom */
#define BRD_EXPL   0x0080    /*   exploding window         */
#define BRD_SYS    0x0100
#define BRD_HSCR   0x0200
#define BRD_VSCR   0x0400
#define BRD_TWIN   0x0800    /*  frame placed inside       */
#define BRD_NEWSH  0x1000
#define BRD_MAX    0x2000
#define BRD_MIN    0x4000
#define BRD_RST    0x6000
#define BRD_MEM    0x8000U   /* especially for PROTOCOL - save gr. in mem. */
void a2tv0brd(int atr,int type,int ldatr,LDSC *ld);

#ifndef TV000
void a2tv0Tumb (int HOR, int SET, int row,int clm, int atr);

#define R_SCROLL 1                /*  right                */
#define L_SCROLL 2                /*  left     WINDOW      */
#define U_SCROLL 3                /*  up       SCROLL      */
#define D_SCROLL 4                /*  down                 */
void a2tv0srl (int dir, int count, int atr, int filler);

/*-----------copy current window to spec pos---------------*/
/*    pos -- dest upper left corner                        */
/*    atr -- -1:with attr, -2:without attr, other:new attr */
void a2tv0cpy (TINT pos, int atr);

int a2tv0chgl (int atr, LDSC *list); /* for list of words from LDSC */
#endif

#define BIGLAT   1           /* for capital Latin  letters */
#define BIGRUS   2           /* for capital Russian letters*/
#define ALLWND   4           /* for whole window           */
#define DIGITS   8           /* for digits                 */
void a2tv0chg (int flags, int atr); /* ---- atr change --- */

#ifndef TV000
/*----------------- text - pointer to first line descript  */
/*                  atr - attribute (color)                */
/*   WRITE TEXT     ulw - window's coord. (ul) in the text */
/*   to window      start - coordinates of start point     */
/*                  stop  - coordinates of stop point      */
int a2tv0wr(LDSC *text,int atr,TINT *ulw,TINT *start,TINT *stop);
#endif

/*---------------------------------------------------------*/
/*-------------------- misc function ----------------------*/
int a2tv0cls(int atr,int filler); /* clear screen          */
int a2tv0blink(int blflg);        /* blink on/off/ask(A2TV.*/
#ifndef TV000
void a2tv0db(void);               /* display buffer        */
void a2tv0ndy(void);              /* NO display (buf only) */
void a2tv0dy(void);               /* display on (+screen)  */
#endif

/*re-count of symbols attributes for B/W, MONO, COLOR4 regimes */
int a2tv0atrf(int on_off); /* on/off/ask (A2TV_ON/OFF/ASK) */

#ifndef TV000
/*------------------------------------------------------------------*/
/*-- set directory for placing graphic windows images --------------*/
/*--    by default current directory of current drive --------------*/
/*------------------------------------------------------------------*/
void a2tv0gpath (char *path);
char *a2tv0gfnm (char *flnm);

/*------------------------------------------------------------------*/
/*reconstruction of graphic window without buffer and file releasing*/
/*------------------------------------------------------------------*/
int a2tv0rsg (void *gdsc);

/*------------------------------------------------------------------*/
/*  reconstruction of graphic window, buffer and file releasing     */
/*------------------------------------------------------------------*/
int a2tv0rsgf (void *gdsc);

/*------------------------------------------------------------------*/
/*  saving of current window in graphic buffer or file (created)    */
/*------------------------------------------------------------------*/
void *a2tv0stgw (int flags, int *rc);

/*------------------------------------------------------------------*/
/*           data updating in existing graphic buffer/file          */
/*------------------------------------------------------------------*/
int a2tv0stg (void *gdsc);

/*------------------------------------------------------------------*/
/* tv Trace interface for a2tv0pc & a2key (a2uline)                 */
/*------------------------------------------------------------------*/
#define A2ROL_MLIN  1
#define A2ROL_CPOS  2
#define A2ROL_TKEY  3
#define A2ROL_WAIT  4
#define A2ROL_MCOL  5
#define A2ROL_MODE  6
#define A2ROL_CSIZ  7
#define A2ROL_TRON (-1)
#define A2ROL_TROF (-2)

#define a2tvttron()            a2tvtrace(A2ROL_TRON, 0,      0,       NULL)
#define a2tvtno a2tvttrof
#define a2tvttrof()            a2tvtrace(A2ROL_TROF, 0,      0,       NULL)
#define a2tvtkey(code)         a2tvtrace(A2ROL_TKEY, code,   0,       NULL)
#define a2tvtwait(msec)        a2tvtrace(A2ROL_WAIT, msec,   0,       NULL)
#define a2tvtmode(mode,fsave)  a2tvtrace(A2ROL_MODE, mode,   fsave,   NULL)
#define a2tvtsiz a2tvtcsiz
#define a2tvtcsiz(size)        a2tvtrace(A2ROL_CSIZ, size,   0,       NULL)
#define a2tvtcur a2tvtcpos
#define a2tvtcpos(pos)         a2tvtrace(A2ROL_CPOS, pos.row,pos.clm, NULL)
#define a2tvtout a2tvtmlin
#define a2tvtmlin(scrp,n,text) a2tvtrace(A2ROL_MLIN, scrp,   n,       text)
#define a2tvtram a2tvtmcol
#define a2tvtmcol(scrp,n,text) a2tvtrace(A2ROL_MCOL, scrp,   n,       text)

void a2tv0TraceLink (void (*tvtrace)(int,int,int,void *));
void a2tvtrace (int type, int i1, int i2, void *p);

/*------------------------------------------------------------------*/
/* mouse interface for a2tv0pc                                      */
/*------------------------------------------------------------------*/
#define A2TV_SM1 (-117)
#define A2TV_SM0 (-118)
#define A2TV_UD0 (-119)

void a2tv0MouseLink (void (*mouse)(int,int,int,int) );
void a2mouseSend (int p1,int p2,int p3,int p4);

void a2tvSMpref (void);
void a2tvSMpost (void);
void a2tvUDpost (void);
void a2tvUDprefGr (int x1,int y1, int x2,int y2);
void a2tvUDprefTx (int c1,int r1, int c2,int r2);
#endif

/*------------------------------------------------------------------*/
/* a2tvMsgOut(msg,atr,row,col,len,flags,limit,keys) -messsage output*/
/*------------------------------------------------------------------*/
/*
 * char *msg - message text
 * int atr   - colors for text and background [-1 std err, -2 std msg]
 * int row   - row for message location [ <0 : ROW=nrows+row ]
 * int col   - column for message location [ <0 : COL=ncols+col,
 *                                        >800: COL=ncols/2+col-1000 ]
 * int len   - message box length [-1 = ncols, -2 = strlen(msg) ]
 * int flags - mode flags [ A2MSG_LEFT,_RIGHT,_CENTR,_NOSAVE,_NOKEY,_CON ]
 * int limit - max waiting time [ 1 = 1/18.2 sec, 91 = 5 sec, ... -1=MAX]
 * long keys - wait keyboard control:
 *             0L - wait for any key pressed;
 *             extended keystroke code - wait for according key pressed;
 *             pointer to list of keystrokes groups,
 *                  -1 groups delimiter, 0 end of list, return group#
 */
#define A2MSG_LEFT   0
#define A2MSG_RIGHT  1
#define A2MSG_CENTR  2
#define A2MSG_NOSAVE 0x100 /* not save backgr */
#define A2MSG_NOKEY  0x200 /* ignore keyboard */
#define A2MSG_CON    0x400 /* cursor on       */

int a2tvMsgOut (char *, int, int,int,int, int, int, long);
void a2tvStrOut (char *msg, int atr, int row,int col,int len);
int a2tvErrOut (char *msg);
int a2tvInfOut (char *msg, int pause);

int a2calcatr (int atr);

#ifndef A2ROLIK
typedef struct {
   /*--------------------------------------------------------*/
   int ulCol,ulRow, nCol, nRow; /* edit window position      */
   unsigned char saveBgr;       /* 0-NO, 1-YES               */
   unsigned char colorTi;       /* title line color          */
   unsigned char colorTx;       /* text color                */
   unsigned char colorSt;       /* status line color         */
   unsigned char colorHl;       /* help color                */
   unsigned char colorSl;       /* selected color            */
   unsigned char TabSz;         /* tab size (default = 8)    */
   unsigned char lockLeft;      /* nonscrolled columns       */
   unsigned char noDialog;      /* show only                 */
   unsigned char useCursor;     /* use cursor for curr pos   */
   unsigned char caseSens;      /* case sensitive search     */
   char search_string[31];
   int  (*file_op)(const char *fname, int oflags);        /* */
   int  (*file_cl)(int fh);                               /* */
   int  (*file_rd)(int fh, V_PTR buf, unsigned count);    /* */
   long (*file_sk)(int fh, long pos, int from);           /* */
   int  (*getkey)(void);                                  /* */
   int  (*prtchr)(int act, int chr);   /* 0-op, 1-prt, -1-cl */
   WORD     BUF_PORTION;         /* 4096 .. 0x7F80           */
   WORD     maxlen;              /* max line length 10..1024 */
   /*--------------------------------------------------------*/
} A2VIEWCFG;

int a2view (char *fname, char *title);
int a2viewCfg (A2VIEWCFG *cfg, char mode); /* 0-get, 1-set */
#endif

#ifdef __cplusplus
}
#endif

# endif /* __FOR_WINDOWS__ */
#endif /* H_A2GRTXT */

